//
//  LGTUpdatePaswordVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/3.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTUpdatePaswordVC: LGTBaseVC {

    var pdLoginView:UIView?
    var passwordTextField:UITextField? //密码
    var passwordNew1TextField:UITextField? //密码
    var passwordNew2TextField:UITextField? //密码
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = ViewBackColor
        self.setBavBarStyle(navBarHiden: false, title: "修改密码", back: true)
        
        
        //密码登陆视图
        self.pdLoginView = UIView.init()
        self.pdLoginView?.backgroundColor = UIColor.white
        self.view.addSubview(self.pdLoginView!)
        self.pdLoginView?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(0)
            make.height.equalTo(SizeFit(150))
            make.top.equalTo(NavBarH() + SizeFit(11))
        })
        
        
        self.passwordTextField = LGTViewCreateUtil.share.createLGTTextFied(font: Font_Normal_14, keyBoardType: UIKeyboardType.default, placeholder: "请输入旧密码", textColor: TextColor, limitCout: 20)
        self.passwordTextField?.isSecureTextEntry = true
        self.pdLoginView?.addSubview(self.passwordTextField!)
        
        let psLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(14), text: "旧密码")
        self.pdLoginView?.addSubview(psLabel)
        psLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.equalTo(SizeFit(14))
            //make.top.equalTo(line3.snp_bottomMargin).offset(KRate * 24)
            make.centerY.equalTo(self.passwordTextField!)
            make.width.equalTo(SizeFit(80))
        }
        
        self.passwordTextField?.snp.makeConstraints({ (make) in
            make.left.equalTo(psLabel.snp_rightMargin).offset(KRate * 14)
            make.top.equalTo(self.pdLoginView!).offset(SizeFitFloat(19))
            make.height.equalTo(KRate * 20)
            //make.centerY.equalTo(psLabel)
            make.right.equalTo(KRate * -100)
        })
        
        let line1:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.pdLoginView?.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(12))
            make.right.equalTo(-SizeFitFloat(12))
            make.top.equalTo(SizeFitFloat(49))
            make.height.equalTo(1)
        }
        
        
        self.passwordNew1TextField = LGTViewCreateUtil.share.createLGTTextFied(font: Font_Normal_14, keyBoardType: UIKeyboardType.default, placeholder: "请输入新密码", textColor: TextColor, limitCout: 20)
        self.passwordNew1TextField?.isSecureTextEntry = true
        self.pdLoginView?.addSubview(self.passwordNew1TextField!)
        
        let psLabel1:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(14), text: "新密码")
        self.pdLoginView?.addSubview(psLabel1)
        psLabel1.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.equalTo(SizeFit(14))
            //make.top.equalTo(line3.snp_bottomMargin).offset(KRate * 24)
            make.centerY.equalTo(self.passwordNew1TextField!)
            make.width.equalTo(SizeFit(80))
        }
        
        self.passwordNew1TextField?.snp.makeConstraints({ (make) in
            make.left.equalTo(psLabel1.snp_rightMargin).offset(KRate * 14)
            make.top.equalTo(self.pdLoginView!).offset(SizeFitFloat(72))
            make.height.equalTo(SizeFitFloat(20))
            //make.centerY.equalTo(psLabel)
            make.right.equalTo(-SizeFitFloat(100))
        })
        
        let line2:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.pdLoginView?.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(12))
            make.right.equalTo(-SizeFitFloat(12))
            make.top.equalTo(SizeFitFloat(99))
            make.height.equalTo(1)
        }
        
        self.passwordNew2TextField = LGTViewCreateUtil.share.createLGTTextFied(font: Font_Normal_14, keyBoardType: UIKeyboardType.default, placeholder: "请再次输入新密码", textColor: TextColor, limitCout: 20)
        self.passwordNew2TextField?.isSecureTextEntry = true
        self.pdLoginView?.addSubview(self.passwordNew2TextField!)
        
        let psLabel2:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(14), text: "确认密码")
        self.pdLoginView?.addSubview(psLabel2)
        psLabel2.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.equalTo(SizeFit(14))
            //make.top.equalTo(line3.snp_bottomMargin).offset(KRate * 24)
            make.centerY.equalTo(self.passwordNew2TextField!)
            make.width.equalTo(SizeFit(80))
        }
        
        self.passwordNew2TextField?.snp.makeConstraints({ (make) in
            make.left.equalTo(psLabel1.snp_rightMargin).offset(KRate * 14)
            make.top.equalTo(self.pdLoginView!).offset(SizeFitFloat(123))
            make.height.equalTo(SizeFitFloat(20))
            //make.centerY.equalTo(psLabel)
            make.right.equalTo(-SizeFitFloat(100))
        })
        
        let line3:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.pdLoginView?.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(12))
            make.right.equalTo(-SizeFitFloat(12))
            make.top.equalTo(SizeFitFloat(149))
            make.height.equalTo(1)
        }
        
        let sureBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "确认修改", font: Font_Normal_18, color: UIColor.white, selectedColor: UIColor.white,corner: SizeFitFloat(22), backColor: YellowColor)
        self.view.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { (make) in
            make.top.equalTo(SizeFit(236) + NavBarH())
            make.height.equalTo(SizeFit(44))
            make.centerX.equalTo(self.view)
            make.width.equalTo(SizeFitFloat(330))
        }
        sureBtn.addTarget(self, action: #selector(sureAction), for: UIControl.Event.touchUpInside)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    @objc func sureAction(){
        
//        if self.passwordTextField?.text?.count ?? 0 < 6{
//            WisdomHUD.showError(text: "请输入6~20位数字或字母的密码")
//            return
//        }
        
        if self.passwordTextField?.text?.count == 0{
            WisdomHUD.showError(text: "请输入旧密码")
            return
        }
        
        if self.passwordNew1TextField?.text?.count == 0{
            WisdomHUD.showError(text: "请输入新密码")
            return
        }
        
        if self.passwordNew2TextField?.text?.count == 0{
            WisdomHUD.showError(text: "请输入确认密码")
            return
        }
        
        if self.passwordNew1TextField?.text != self.passwordNew2TextField?.text{
            WisdomHUD.showError(text: "密码不一致")
            return
        }
        
        let old_password:String = LGTOCBaseUtil.md5(forLower32Bite: self.passwordTextField?.text ?? "")
        let new_password:String = LGTOCBaseUtil.md5(forLower32Bite: self.passwordNew1TextField?.text ?? "")
        let param = ["old_password":old_password, "new_password":new_password]
        weak var weakSelf = self
        LGTHttpUtil.httpUtil.requestData(url: Update_Pass_URL, param: param, showMessage: true, success: { (json) in
            weakSelf?.navigationController?.popViewController(animated: true)
            
        }) { (json) in
            
        }
    }
}
